package controllers;

import helpers.CrawlerHelper;
import helpers.UrlHelper;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class CheckUrlEncodeDecodeServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		TreeMap<String, String> check = new TreeMap<String, String>();
		String url = (String) request.getParameter("url");
		String type = (String) request.getParameter("type");
		
		if (url == null || !UrlHelper.checkDomain(url)) {
			check.put("name", "Please provice your domain.");
		} else {
			url = UrlHelper.getMainDomain(url);
		} 
		
		//Cache header
		response.addHeader("Cache-Control", "public, max-age=60000");
		
		request.setAttribute("check", check);
		request.setAttribute("url", url);
		request.setAttribute("type", type);
		try {
			request.getRequestDispatcher("/pages/check_encode_decode.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
}
